package com.xonro.common.histrans.sfeg.biz.custom;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.zz.biz.common.DrpProcessTransBiz;
import com.xonro.common.histrans.zz.ctx.DrpProcessCreateContext;
import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhuhj
 * @Description: 客户信息登记（2.0）
 * @Date: 2023/12/26 18:12
 * @Version: 1.0
 */
public class CompanyApplyBiz extends DrpProcessTransBiz {
    @Override
    protected void transProcess0(DrpProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();
        SDK.getLogAPI().consoleInfo("获取客户信息数据");

        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_EU_COMPANY_APPLY");//客户信息
        logModel.setOuterTable("ct_BPM_CustInfoRegMaintain");//基础项目信息表
        logModel.setOuterPK(mainData.getString("MyId"));
        logModel.setOuterExt1(mainData.getString("CustomerName"));//团队名称
        logModel.setOuterExt2(mainData.getString("BillNo"));//单据号
        BOCreateData mainBOCreateData = new BOCreateData();
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBoName("BO_EU_COMPANY_APPLY");
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);

        mainBO.set("DATA_TYPE","1");//历史数据
        mainBO.set("DRP_ID",mainData.getString("MyId"));
        mainBO.set("APPLY_NO",mainData.getString("BillNo"));
        mainBO.set("APPLY_DATE",mainData.getString("CreateTime"));
        mainBO.set("APPLY_USER",mainData.getString("CreateName"));
        mainBO.set("APPLY_USER_ID",mainData.getString("CreateId"));
        mainBO.set("APPLY_DEPT",mainData.getString("CreateDept"));
        mainBO.set("APPLY_DEPT_ID",mainData.getString("CreateDeptId"));


        mainBO.set("BU_CENTER","");
        mainBO.set("BU_UNIT","");

        mainBO.set("YEAR","");
        mainBO.set("MONTH","");
        mainBO.set("CUSTOMER_NAME",mainData.getString("CustomerName"));
        mainBO.set("ID_CARD",mainData.getString("TaxRegistrationNum"));
mainBO.set("INDUSTRY_TYPE",mainData.getString("CustomerProfileId"));
mainBO.set("COMPANY_TYPE",mainData.getString("CompanyType"));
mainBO.set("LEGAL_USER_NAME",mainData.getString("Legalbody"));
mainBO.set("REGIST_AMOUNT",mainData.getString("Regfound"));
mainBO.set("CITY","");
mainBO.set("DETAIL_ADDRESS",mainData.getString("Address"));
mainBO.set("ADDRESS",mainData.getString("City"));
        mainBO.set("CONTRACT",mainData.getString("Contactpsn"));
        mainBO.set("PHONE",mainData.getString("Phone"));
        mainBO.set("RANG",mainData.getString("BusinessScope"));
        mainBO.set("STATUS",mainData.getString("CustomerStatus"));
        mainBO.set("F_LQBHTGFY","");
        mainBO.set("TIME_LIMIT_END",mainData.getString("EndTime"));
        mainBO.set("TIME_LIMIT_STARY",mainData.getString("StartTime"));
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        fileTransData.setFileJson(mainData.getString("File"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        fileTransData.setBoName("BO_EU_COMPANY_APPLY");
        fileTransData.setFieldName("FJ");
        fileTransData.setUserId("admin");
        fileTransData.setAppId("com.awspaas.user.apps.zhongzhuang.mis");
        fileTransData.setCreateDate(new Timestamp(mainData.getDate("CreateTime").getTime()));
    }


    @Override
    protected List<RowMap> getMainDatas() {
        return SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM ct_BPM_CustInfoRegMaintain");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }


    @Override
    protected String getDrpType() {
        return null;
    }

    @Override
    protected String getProcessDefId() {
        return "obj_be0d2f46a1724286b0e2327b1816cd66";
    }

    @Override
    protected String getActivityDefId() {
        return "obj_d57900651d8f44b6919b8edca2ed736b";
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        String billNo = ctx.getMainData().getString("BillNo");//单据号
        return "客户信息登记流程历史数据同步"+billNo;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        return mainData.getString("Wiid");
    }

    @Override
    protected String getOuterTable(DrpProcessCreateContext historyTransContext) {
        return "ct_BPM_CustInfoRegMaintain";
    }

    @Override
    protected String getPk(DrpProcessCreateContext historyTransContext) {
        RowMap mainData = historyTransContext.getMainData();
        return mainData.getString("MyId");
    }
}
